package ru.spbu.math.android.cbrf.dataprovider.handlers;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ru.spbu.math.android.cbrf.model.Rate;
import ru.spbu.math.android.cbrf.model.StockItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Class for parsing dynamic data from CBRF and put it in the List of Rate This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

public class CurrencyDynamicSaxHandler extends DefaultHandler {

    private final String VALUE = "Value";
    private final String RECORD = "Record";
    private final String DATE = "Date";

    private List<Rate> currList;
    private Rate currency;
    private StringBuilder builder;
    private StockItem source;
    private StockItem target;


    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (this.currency != null) {
            if (localName.equalsIgnoreCase(VALUE)) {
                currency.setValue(getDoubleFromString(builder.toString()));
            } else if (localName.equalsIgnoreCase(RECORD)) {
                currList.add(currency);
            }

            builder.setLength(0);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        currList = new ArrayList<Rate>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(RECORD)) {
            this.currency = new Rate();
            currency.setDate(attributes.getValue(DATE));
            currency.setSource(source);
            currency.setTarget(target);
        }
    }

    /**
	 * Return currency list from XML
	 * @return  List<Currency>
	 */
    public List<Rate> getCurrency() {
        return currList;
    }

    /**
	 * Set source for CurrencyRate
	 * @param  sourceItem
	 */
    public void setSource(StockItem sourceItem) {
        source = sourceItem;
    }
    
    /**
	 * Set target for CurrencyRate
	 * @param  targetItem
	 */
    public void setTarget(StockItem targetItem) {
        target = targetItem;
    }

    //Take double from specified string

    private static double getDoubleFromString(String line) {
        String str = "";
        boolean start = false;
        double result = 0;

        for (int i = 0; i < line.length(); i++) {
            if (Character.isDigit(line.charAt(i))) {
                start = true;
                str += line.charAt(i);
            } else if (line.charAt(i) == '.' || line.charAt(i) == ',') {
                str += '.';
            }
        }
        if (start) {
            result = Double.parseDouble(str);
        } else {
            result = 0;
        }

        return result;

    }
}
